package com.haoziqi.chapter_06;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * description
 * created by A on 2021/3/5
 */
public class Flink08_AdClick {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 1.读取数据
        SingleOutputStreamOperator<AdsClickLog> adClickDS = env
                .readTextFile("input/AdClickLog.csv")
                .map(ad -> {
                    String[] datas = ad.split(",");
                    return new AdsClickLog(
                            Long.valueOf(datas[0]),
                            Long.valueOf(datas[1]),
                            datas[2],
                            datas[3],
                            Long.valueOf(datas[4])
                    );
                });

        // 2.处理数据： 各省份 各广告 的点击量
        // 按照 统计维度 分组： 省份、广告
        adClickDS.map(new MapFunction<AdsClickLog, Tuple2<String,Long>>() {
            @Override
            public Tuple2<String, Long> map(AdsClickLog value) throws Exception {
                return Tuple2.of(value.getProvince()+"_"+value.getAdId(),1L);
            }
        }).keyBy(datas -> datas.f0).sum(1).print();


        env.execute();
    }
}
